home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
neuralwa
/
nw2v50
/
noiseio.c
< prev
next >
Wrap
Text File
|
1993-08-23
|
20KB
|
792 lines
/* noiseio.c */
/************************************************************************
* Copyright(C) 1987-1992 NeuralWare Inc *
* Penn Center West, IV-227, Pittsburgh, PA 15276 *
* Telephone: (412) 787-8222 FAX: (412) 787-8220 *
* *
* All rights reserved. No part of this program may be reproduced, *
* stored in a retrieval system, or transmitted, in any form or by any *
* means, electronic, mechanical, photocopying, recording or otherwise *
* without the prior written permission of the copyright owner, *
* NeuralWare, Inc. *
* *
* PROPRIETARY NOTICE *
* *
* This document is the property of NeuralWare, Inc. and contains *
* trade-secrets and other proprietary information. The information *
* herein is reserved as proprietary to NeuralWare, and is not to be *
* published, reproduced, copied, disclosed, used, or reverse *
* engineered without the express written consent of a duly authorized *
* representative of NeuralWare. *
************************************************************************
*/
/*******************************************************************/
/* */
/* noiseio.c - Sample User IO Program */
/* */
/* Author: John C. Fairman August 31, 1990 */
/* Copyright 1990 by NeuralWare Inc. */
/* */
/* $Author : $ */
/* $Date : $ */
/* $Source : $ */
/* $Revision : $ */
/* $Log : $ */
/* */
/*******************************************************************/
#define UIO_SERVER 1
#define SERVER_EMULATOR_FILE 1
#define SKIP_COMPAT_DATA 1
#include "userutl.h"
#include <math.h>
#include <string.h>
#ifndef SUN
#ifndef DLC
#include <stdlib.h>
#endif
#endif
#include "uio_strc.h"
#include "uio_tran.h"
#include "uio_reqs.pro"
#include "uio_cmds.pro"
#ifdef MAC
#include "macuio.redef"
#endif
#ifdef MAC
#define LINE_COLOR 15
#undef FASTRAND
#else
#define LINE_COLOR 13
#define FASTRAND
#endif
int ReadTS ARGLIST(( char * ));
/************************************************************************
*
* Noise Filtering Example: Input / Output Routine
*
************************************************************************
This routine reads a set of data values from an input file. It assumes
that there is one measurement per input line. The data has been
pre-scaled using "conv".
During training, a block of 29 inputs is randomly selected from the
entire time-series. This is applied as both the input and the output
for the network. The network is treated as an "auto-associative"
network.
During recall, the time-series is stepped through sequentially. Only
the middle processing element of the output layer is plotted on the
graph.
*/
/* */
/************************************************************************
*
* Window management
*
************************************************************************
*/
#define inTrain 3 /* training window inner border */
#define inWMSE 4 /* mean square error inner border */
/************************************************************************
*
* Learning Error display
*
************************************************************************
*/
#define ENPts 491 /* # of error points */
#define EYVals 90 /* # of y-values */
int EX0 = 0; /* x-coord of lower left corner */
int EY0 = 0; /* y-coord of lower left corner */
int ECtr = 0; /* current "data" item */
double EXScale = 0.0; /* x-scale factor */
double EXOffset = 0.0; /* error offset */
float EVals[ENPts] = {0}; /* error values */
int EVFlag[ENPts] = {0}; /* error value valid flag */
int *null_ptr = 0; /* null pointer for uSoft 5.0 */
void ShowError() /* plot out the error array */
{
int n; /* index into the data array */
int dx; /* draw index */
int pdx; /* previous draw index */
/* put up a header */
/* plot the data */
pdx = 0;
for( n = 0; n < ENPts; n++ ) {
if ( EVFlag[n] == 0 ) {
pdx = 0;
continue;
}
dx = EXScale * (EVals[n] - EXOffset);
if ( dx >= EYVals ) dx = EYVals - 1;
if ( dx < 0 ) dx = 0;
if ( n == 0 || pdx == 0 || pdx == dx )
ug_point( inWMSE, LINE_COLOR, 0, EX0+n, EY0+dx );
else ug_line( inWMSE, LINE_COLOR, 0, EX0+n, EY0+pdx, EX0+n, EY0+dx, 0 );
pdx = dx;
}
/* show where next data point goes */
ug_line( inWMSE, 15, 0, EX0+ECtr, EY0+1, EX0+ECtr, EY0+EYVals, 0 );
}
void NextError( v ) /* plot the next error datum */
double v; /* value of error to plot */
{
int dx; /* y-coordinate (relative) of error */
int pdx; /* previous y-coord */
double pv; /* previous v */
/* save the item for posterity */
EVals[ECtr] = v;
EVFlag[ECtr] = 1;
/* compute where the point should be plotted */
dx = (v - EXOffset) * EXScale;
if ( dx < 1 ) dx = 1;
if ( dx >= EYVals ) dx = EYVals - 1;
/* clear old cursor out */
ug_line( inWMSE, 0, 0, EX0+ECtr, EY0, EX0+ECtr, EY0+EYVals, 0 );
/* plot new data point */
if ( ECtr != 0 && EVFlag[ECtr-1] != 0 ) {
pv = EVals[ECtr-1];
pdx = (pv - EXOffset) * EXScale;
if ( pdx < 0 ) pdx = 0;
if ( pdx > EYVals-1 ) pdx = EYVals-1;
} else pdx = 0;
if ( ECtr == 0 || EVFlag[ECtr-1] == 0 )
ug_point( inWMSE, LINE_COLOR, 0, EX0+ECtr, EY0+dx );
else ug_line( inWMSE, LINE_COLOR, 0, EX0+ECtr, EY0+pdx, EX0+ECtr, EY0+dx, 0 );
/* compute next cursor location */
ECtr++;
if ( ECtr >= ENPts ) ECtr = 0;
/* draw new cursor line */
ug_line( inWMSE, 15, 0, EX0+ECtr, EY0, EX0+ECtr, EY0+EYVals, 0 );
}
/* */
/************************************************************************
*
* Common data & statics
*
************************************************************************
*/
/* data set definitions */
#define NTPTS 491 /* number of pts in data file */
#define SetSize 450 /* size of training set */
float TrainSet[NTPTS+1] = {0.0}; /* current training set */
float TrainRef[NTPTS+1] = {0.0}; /* reference training data */
int SampX[NTPTS+1] = {0}; /* sample array */
int SX = -1; /* index into sample array */
int InputLen= 0; /* input length */
int TSetL = 0; /* training set length */
int TrainSz = 30; /* training samples per pass */
int ToTrain = 0; /* # of items still to train */
long Pass = 0; /* pass number */
int DataCtr = 0; /* data counter */
int NextFlag= 0; /* next cycle flag */
float LastDR = 0.0; /* last desired result */
float MSE = 0.0; /* mean square error for a data set */
int x = 0; /* previous x-coordinate */
int ynew = 0; /* current y-coordinate */
int yold = 0; /* previous y-coordinate */
int yout = 0; /* network out y-coordinate */
int xlag = 14; /* first x-coordinate for network out */
/* window position and sizes */
#define XBorder 3
#define YBorder 3
#define InTX0 70
#define InTY0 10
#define InTX1 (InTX0+SetSize+1)
#define InTY1 (InTY0+EYVals)
#define InMX0 (InTX0)
#define InMY0 (InTY1+3*YBorder)
#define InMX1 (InMX0+SetSize+1)
#define InMY1 (InMY0+EYVals)
/* */
/************************************************************************
*
* Plot the training set
*
************************************************************************
*/
PlotTrain()
{
int x;
int wx;
int yold, ynew;
yold = 0;
x = 0;
for (wx = 0; wx < TSetL; wx++) {
/* plot the values in the Training Set */
ynew = TrainSet[wx] * EYVals;
if ( x == 0 )
ug_point( inTrain, 13, 0, x, ynew );
else ug_line (inTrain, 13, 0, x, yold, x, ynew, 0);
x++;
yold = ynew;
}
}
/* */
/************************************************************************
*
* Training Set Input / Check it is there Routines
*
************************************************************************
*/
int IsTSOk()
{
if ( TSetL <= 0 ) return( UIO_ERROR );
else return( UIO_OK );
}
int ReadTS( buf )
char *buf; /* work buffer */
{
FILE *fp; /* input file pointer */
char *sp; /* input string */
int c; /* work character */
int wx; /* work index */
SX = -1;
TSetL = 0;
for(;;) {
PutStr ("What is the training input File (<RETURN> for default) ?");
sp = GetStr();
while( *sp && *sp <= ' ' ) sp++; /* skip leading white space */
if ( *sp == '\0' ) {
sp = "noisetst.1"; /* default file name */
}
fp = fopen (sp, "r");
if (fp == (FILE *)null_ptr ) {
sprintf( buf, "Could not open <%s>\n", sp );
PutStr( buf );
continue;
}
for ( wx = 0; wx < NTPTS;) {
if ( fgets( &buf[0], 80, fp ) == 0 ) break;
/* kill any comments */
for( sp = &buf[0]; (c = *sp) != '\0'; sp++ ) {
if ( c == '!' || c == '*' || c == '\r' || c == '\n' )
break;
}
*sp = '\0'; /* kill comments, etc */
/* skip leading space and check for blank lines */
for( sp = &buf[0]; *sp && *sp <= ' '; ) sp++;
if ( *sp == '\0' )
continue;
sscanf( buf, "%f", &TrainSet[wx] );
TrainRef[wx] = TrainSet[wx];
wx++;
}
TSetL = wx;
fclose(fp);
if ( TSetL == 0 ) {
PutStr( "Nothing in data set, try again\n" );
continue;
}
break;
}
return( 0 );
}
/* */
static int ModeFlag = 0; /* 0=do not write to file; 1=write to file */
static int InitFlag = 0;
FILE *OutFp = 0; /* output file pointer */
#if ((SUN || VAXULT) && !SYSV) || IRIS
extern long random();
#define rand random
#define MAXRAND (0x7fffffffl)
#else
extern int rand(); /* random number generator */
#ifdef VMS
#define MAXRAND (0x7fffffffl)
#else
#define MAXRAND (0x7fff)
#endif
#endif
OpenAllWindows( )
{
ug_window( inTrain, 0, InTX0, InTY0, InTX1, InTY1 );
ug_window( inWMSE, 0, InMX0, InMY0, InMX1, InMY1 );
InitFlag = 1;
}
/*******************************************************************/
/* */
/* Functions necessary for handling the User I/O package itself. */
/* */
/*******************************************************************/
/* FUNCTION */
NINT UIO_Init(file)
TEXT *file;
{
OpenAllWindows( );
}
/**/
/* FUNCTION */
NINT UIO_Term(process)
NINT process;
{
if ( OutFp != (FILE *)null_ptr ) fclose( OutFp );
PutStr( "bye bye\n" );
return(UIO_OK);
}
/* FUNCTION */
NINT UIO_Attention()
{
TEXT *sp;
InitFlag = 0;
if ( OutFp != (FILE *)null_ptr ) fclose( OutFp );
OutFp = (FILE *)0;
PutStr( "File to write to or <RETURN> for none? " );
sp = GetStr();
if ( *sp > ' ' ) {
OutFp = fopen( sp, "w" ); /* just write */
if ( OutFp == (FILE *)null_ptr )
PutStr( "Could not open output file\n" );
}
OpenAllWindows( );
return(UIO_OK);
}
/*******************************************************************/
/* */
/* Functions necessary for handling a learning session. */
/* */
/*******************************************************************/
/* FUNCTION */
NINT UIO_Learn_Start()
{
TEXT *sp; /* string pointer */
float v; /* work float */
TEXT buf[100]; /* work buffer */
NINT wx; /* work index */
if (!InitFlag) OpenAllWindows( );
if ( TSetL <= 0 )
ReadTS( &buf[0] ); /* read training data */
if ( IsTSOk() ) return(UIO_ERROR); /* exit if error */
/* set up the window & tell the user what to do */
ug_winclr( 0 ); /* clear windows */
ug_puts( inWMSE, 7, 0, XBorder,YBorder, "Mean Square Error", 0 );
ug_puts( inTrain, 7, 0, XBorder,YBorder, "Raw Input Data", 0 );
for( wx = 0; wx < NTPTS; wx++ ) TrainSet[wx] = TrainRef[wx];
PlotTrain();
ToTrain = TrainSz-1;
/* ask the user for the scale factor for errors */
PutStr( "What is the maximum expected error? " );
sp = GetStr();
v = 0.0;
sscanf( sp, "%f", &v );
if ( v < 0.0001 ) {
if ( EXScale < .0001 )
EXScale = EYVals / 2.0;
} else {
EXScale = EYVals / v;
}
/* display the errors as they stand */
ShowError();
DataCtr = 0;
MSE = 0.0;
Pass = 0;
return(UIO_OK);
}
/**/
/* FUNCTION */
NINT UIO_Learn_Input(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT wx; /* work index */
int wn; /* work number */
int ModVal; /* modulo value */
if (!InitFlag) OpenAllWindows( );
if ( IsTSOk() ) return(UIO_ERROR); /* exit on error */
if ( nPEs > InputLen ) InputLen = nPEs;
/* zero out the input vector */
for( wx = 0; wx < nPEs; wx++ ) Datap[wx] = 0.0;
#ifdef FASTRAND
/* added int cast for MS-C 7.0; 4-28-93 mjs */
#ifdef MSC
wn = abs( (int)(((long)rand()) % (TSetL - nPEs)) );
#else
wn = abs( ((long)rand()) % (TSetL - nPEs) );
#endif
#else
/* pick next training input randomly */
ModVal = TSetL - nPEs;
if ( SX < 0 || SX >= ModVal ) {
for( SX = 0; SX < ModVal; SX++ ) {
RandAgain:
wn = rand() % ModVal;
for( wx = 0; wx < SX; wx++ ) {
if ( wn == SampX[wx] ) goto RandAgain;
}
SampX[SX] = wn;
}
SX = 0;
}
wn = SampX[SX++];
#endif
for (wx = 0; wx < nPEs; wx++, wn++)
Datap[wx] = TrainSet[wn];
/*desired response*/
LastDR = Datap[nPEs/2];
return(UIO_OK);
}
/* FUNCTION */
NINT UIO_Learn_Output(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT wx; /* work index */
if (!InitFlag) OpenAllWindows( );
wx = 0;
Datap[wx++] = LastDR; /* set data */
while( wx < nPEs ) Datap[wx++] = 0.0; /* no data for these */
return(UIO_OK);
}
/**/
/* FUNCTION */
NINT UIO_Learn_Result(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
float v; /* work float */
TEXT buf[100]; /* work buffer */
if (!InitFlag) OpenAllWindows( );
v = Datap[nPEs/2] - LastDR; /* error */
MSE += (v * v); /* mean square error */
if ( --ToTrain <= 0 ) {
MSE = sqrt(MSE)/TrainSz;
Pass++;
sprintf( buf, "pass = %ld, MSE = %.3f\n", Pass, MSE );
PutStr( buf );
NextError( MSE );
MSE = 0.0;
ToTrain = TrainSz;
}
return(UIO_OK);
}
/* FUNCTION */
NINT UIO_Learn_End()
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/**/
/*******************************************************************/
/* */
/* Functions necessary for handling a recall or testing session. */
/* */
/*******************************************************************/
/* FUNCTION */
NINT UIO_Recall_Start()
{
char *sp; /* string pointer */
float v; /* work float */
TEXT buf[100]; /* work buffer */
NINT wx; /* work index */
if (!InitFlag) OpenAllWindows( );
ReadTS( &buf[0] ); /* read new test data */
if ( IsTSOk() ) return(UIO_ERROR); /* no good */
PutStr( "How Much Noise (0-100%)? " );
sp = GetStr();
if ( *sp == 0 ) v = 0.0;
else sscanf( sp, "%f", &v );
v = .01 * v;
for( wx = 0; wx < NTPTS; wx++ )
TrainSet[wx] = TrainRef[wx] + (v * (((2.0*rand())/MAXRAND)-1.));
DataCtr = 0;
ug_winclr( 0 );
PlotTrain();
ug_puts( inWMSE, 7, 0, XBorder,YBorder, "Filtered Output Signal", 0 );
ug_puts( inTrain, 7, 0, XBorder,YBorder, "Noisy Input Signal", 0 );
yold = 0;
xlag = 0;
return(UIO_OK);
}
/* FUNCTION */
NINT UIO_Read(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT wx; /* work index */
if (!InitFlag) OpenAllWindows( );
if ( IsTSOk() ) return(UIO_ERROR); /* exit on error */
if ( nPEs > InputLen ) InputLen = nPEs;
if ( xlag == 0 ) xlag = InputLen / 2;
/* zero out the input vector */
for (wx = 0; wx < nPEs; wx++) Datap[wx] = 0.0;
/* select the next string from the data set */
for (wx = 0; wx < nPEs; wx++)
Datap[wx] = TrainSet[wx + DataCtr];
LastDR = Datap[nPEs/2];
return(UIO_OK);
}
/**/
/* FUNCTION */
NINT UIO_Write(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
int wx; /* work index */
NINT ret_val = UIO_OK;
if (!InitFlag) OpenAllWindows( );
if ( OutFp != (FILE *)null_ptr )
fprintf( OutFp, "%.5f%s", Datap[nPEs/2], NEW_LINE_STR );
yout = Datap[nPEs/2] * EYVals;
if ( yout >= EYVals ) yout = EYVals - 1;
if ( yout < 0 ) yout = 0;
if (DataCtr == 0)
ug_point(inWMSE,LINE_COLOR,0,xlag,yout);
else ug_line( inWMSE,LINE_COLOR,0,xlag,yold,xlag,yout, 0);
#if 0
wx = LastDR * EYVals;
if ( wx >= EYVals ) wx = EYVals - 1;
if ( wx < 0 ) wx = 0;
ug_point( inWMSE, 6,0,xlag,wx);
#endif
xlag++;
yold = yout;
DataCtr++;
if(DataCtr >= (TSetL - InputLen * 2)) {
DataCtr = 0;
ret_val = UIO_ERROR;
if ( OutFp != (FILE *)null_ptr ) {
fclose( OutFp );
OutFp = (FILE *)0;
}
PutStr("Hit <RETURN> to continue");
GetStr();
}
return(ret_val);
}
/* FUNCTION */
NINT UIO_Write_Step(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/**/
/* FUNCTION */
NINT UIO_Recall_Test(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT wx; /* work index */
if (!InitFlag) OpenAllWindows( );
wx = 0;
Datap[wx++] = LastDR; /* set data */
while( wx < nPEs ) Datap[wx++] = 0.0; /* no data for these */
return(UIO_OK);
}
/* FUNCTION */
NINT UIO_Recall_End()
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/**/
/*******************************************************************/
/* */
/* Other miscelaneous functions. */
/* */
/*******************************************************************/
/* FUNCTION */
NINT UIO_Instrument(Instrument_id, nDataElems, DataElemp)
NINT Instrument_id;
NINT nDataElems;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/* FUNCTION */
NINT UIO_ObjFunc(eoeflag, DataElemp)
NINT eoeflag;
SREAL *DataElemp;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/* FUNCTION */
NINT UIO_Rewind()
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}
/**/
/* FUNCTION */
NINT UIO_Explain(LayN, nPEs, Datap)
NINT LayN;
NINT nPEs;
SREAL *Datap;
{
NINT ret_val = UIO_OK;
/* USER TO PLACE CODE HERE */
return(ret_val);
}